package org.fhm.zdte.common.pool;


import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * A custom periodic thread pool.
 *
 * @author 谭波
 * @since 2023/11/4
 */
class ScheduledThreadPool extends ScheduledThreadPoolExecutor {

    /**
     * Configure thread pool.
     *
     * @param corePoolSize     core
     * @param threadPoolPrefix the prefix of thread pool name.
     * @param maximumPoolSize  the max thread size.
     * @param keepAliveTime    the thread alive time.
     * @param handler          the reject handler.
     * @see ScheduledThreadPoolExecutor
     */
    public ScheduledThreadPool(
            int corePoolSize,
            String threadPoolPrefix,
            int maximumPoolSize,
            long keepAliveTime,
            RejectedExecutionHandler handler
    ) {
        super(corePoolSize, new CustomThreadFactory(threadPoolPrefix));
        this.setRejectedExecutionHandler(handler);
        this.setKeepAliveTime(keepAliveTime, TimeUnit.SECONDS);
        this.setMaximumPoolSize(maximumPoolSize);
        this.allowCoreThreadTimeOut(true);
        this.setRemoveOnCancelPolicy(true);
    }
}
